//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    systemInfo:{},
    openId: '',
  },
  //结算
  jiesuan:function(){
    wx.navigateTo({
      url: '../my/jiesuan/index?id=39'
    })
  },
  //订单
  dingdan: function () {
    wx.navigateTo({
      url: '../my/myorder/index'
    })
  },
  //订单
  dingdan1: function () {
    wx.navigateTo({
      url: '../my/order/index'
    })
  },
  //订单
  dizhi: function () {
    wx.navigateTo({
      url: '../my/addressList/index'
    })
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../android/index'
    })
  },
  //安卓验证
  android_1: function () {
    wx.navigateTo({
      url: '../android_1/index'
    })
  },
  history:function(){
    wx.navigateTo({
      url: '../journal/index'
    })
  },
  //事件处理函数
  toTest: function () {
    console.log(this.data.systemInfo)
    if (this.data.systemInfo.platform=='ios'){
      wx.navigateTo({
        url: '../ios/index'
      })
    }else{
      wx.navigateTo({
        url: '../android/index'
      })
    }
  },
  onLoad: function () {
    wx.getNetworkType({
      success: function (res) {
        console.log(res)
        console.log(res.networkType)
      }
    })
    var that = this
    this.phoneModel();
    wx.login({
      success: function (res) {
        if (res.code) {
          console.log("获取到code",res.code)

          wx.request({
            url: app.globalData.the_host + '/auth/findOpenId',
            data: { "code": res.code},
            header: {
              'content-type': 'application/json'
            },
            method: "POST",
            success: res => {

              if (res.statusCode == 200) {
                if (res.data) {
                  app.globalData.openId = res.data.data;
                  that.setData({
                    openId: res.data.data,
                  })
                  that.dataIP();
                  console.log("获取到openId", app.globalData.openId)
                }
              }

            }
          }) 

        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })

      // console.log(app.globalData.userInfo, this.data.userInfo)
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })

        // console.log(app.globalData.userInfo, this.data.userInfo)
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          // console.log(app.globalData.userInfo, this.data.userInfo)
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  // 判断手机型号
  phoneModel: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        that.setData({
          systemInfo: res,
        })
       
      }
    })
  },
  /**
   * 获取购物车数量
   */
  dataIP: function () {
    var that = this;
    wx.request({
      url: 'http://ip-api.com/json',
      success: function (res) {
        console.log("返回数据===>", res.data);
        app.globalData.ip = res.data.query;
        that.setData({
          ip: res.data.query,
        })
      }
    });
  },
})